﻿using LibraryManagement.Custom_Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibraryManagement.Profile_Management.Setup
{
    public partial class MasterBranch : libForm
    {
        private LibManagementEntities context;
        private Branch OBranch;
        private enum UpdateType { New = 0, Edit = 1 }
        UpdateType eUpdateType;
        public MasterBranch()
        {
            InitializeComponent();
            context = new LibManagementEntities();
        }

        private void lsbBranch_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lsbBranch.SelectedIndex != -1)
            {
                try
                {
                    OBranch = context.Branches.Single(x => x.BranchId == ((Branch)lsbBranch.SelectedItem).BranchId);
                    txtBranchName.Text = OBranch.BranchName;
                    txtBranchShortName.Text = OBranch.BranchShortName;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void MasterBranch_Load(object sender, EventArgs e)
        {
            txtBranchName.Enabled = false;
            txtBranchShortName.Enabled = false;
            btnSave.Enabled = false;
         
            lsbBranch.SetDataSource(context.Branches.ToList(), "BrnachName", "BranchId");
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            btnEdit.Enabled = false;
            txtBranchName.Enabled = true;
            txtBranchShortName.Enabled = true;
            lsbBranch.Enabled = false;
            btnSave.Enabled = true;
            eUpdateType = UpdateType.New;
            txtBranchShortName.Clear();
            txtBranchName.Clear();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                context = new LibManagementEntities();
                btnNew.Enabled = true;
                btnEdit.Enabled = true;
                lsbBranch.Enabled = true;
                if (eUpdateType == UpdateType.New)
                {
                    OBranch = new Branch();
                    OBranch.BranchName = txtBranchName.Text;
                    OBranch.BranchShortName = txtBranchShortName.Text;
                    context.Branches.Add(OBranch);
                }
                else if (eUpdateType == UpdateType.Edit)
                {
                    OBranch.BranchName = txtBranchName.Text;
                    OBranch.BranchShortName = txtBranchShortName.Text;
                }
                context.SaveChanges();
                btnClear_Click(sender, e);
                MasterBranch_Load(sender, e);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtBranchName.Clear();
            txtBranchShortName.Clear();
            txtBranchName.Enabled = false;
            txtBranchShortName.Enabled = false;
            btnEdit.Enabled = true;
            btnSave.Enabled = true;
            btnNew.Enabled = true;
            lsbBranch.Enabled = true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            btnNew.Enabled = false;
            lsbBranch.Enabled = false;
            txtBranchName.Enabled = true;
            txtBranchShortName.Enabled = true;
            btnSave.Enabled = true;
            eUpdateType = UpdateType.Edit;
        }
    }
}
